//
//  ChooseHotBrandCell.m
//  CarUser
//
//  Created by 王森林 on 2018/12/7.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "ChooseHotBrandCell.h"
#import "ChooseResultViewController.h"
//顶部文字
@interface  CommonTitleView()

@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *titleLabel;




@end


@implementation CommonTitleView

- (instancetype) initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
    
}

- (void)createUI
{
    [self  removeAllSubviews];
    [self addSubview:self.lineLabel];
    [self addSubview:self.titleLabel];
    [self addSubview:self.moreBtn];
    [self createConstraint];
    
}


- (void)handleMoreAction
{
    if (self.resultBlock) {
        self.resultBlock(@"");
    }
    
    
}

- (void)createConstraint
{
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.mas_left).offset(15);
        make.width.mas_equalTo(4);
        make.height.mas_equalTo(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.lineLabel.mas_right).offset(4);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(15);
    }];
    
    
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(15);
    }];
    
}


- (void)setTitleName:(NSString *)titleName
{
    if (titleName) {
        self.titleLabel.text = titleName;
    }
    
}

- (UILabel *)lineLabel
{
    if (!_lineLabel) {
        _lineLabel = [UILabel new];
        _lineLabel.backgroundColor = Common_TextLowColor;
        _lineLabel.layer.cornerRadius = 3;
        _lineLabel.layer.masksToBounds = YES;
        
    }
    return _lineLabel;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = B_FONT_SIZE(16);
        
    }
    return _titleLabel;
    
}

- (UIButton *)moreBtn
{
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        [_moreBtn setImage:IMG(@"home_arrow") forState:UIControlStateNormal];
        [_moreBtn setTitleColor:Common_TextLowColor forState:UIControlStateNormal];
        _moreBtn.titleLabel.font = FONT_SIZE(14);
        //        _a_detaiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        //        //    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
        [_moreBtn setTitleEdgeInsets:UIEdgeInsetsMake(0 ,0, 0.0,0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
        [_moreBtn setImageEdgeInsets:UIEdgeInsetsMake( 0, 0,0.0, -90)];//图片距离右边框距离减少图片的宽度，其它不边
        [_moreBtn addTarget:self action:@selector(handleMoreAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
    
}


@end


@interface ChooseHotBrandCell()


@property (nonatomic, strong) NSMutableArray *dataAry;
@end

#define BTNTAG 100
@implementation ChooseHotBrandCell






- (void)setData:(id)data
{
    if (data && self.dataAry.count == 0) {
        [self.dataAry removeAllObjects];
        [self.dataAry addObjectsFromArray:data];
        [self createUIWithData];
    }
    
}


- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag = btn.tag - BTNTAG;
    
    NSDictionary *dict = self.dataAry[tag];
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.type = @"3";
      controller.pushType = 1;
    controller.SERIES_ID = [dict objectForKey:@"BRAND_ID"];
    [[self  viewController].navigationController pushViewController:controller animated:YES];
    
}



-(void)createUIWithData
{
    [self.contentView removeAllSubviews];
    [self.contentView addSubview:self.titleView];
    self.titleView.titleName = @"热门品牌";

    [self createConstraint];
    
    __block float x = 15;
    __block float y = 42;
    __block float btnWidth  = 60;
    __block float spaceX = (BOUNDWIDTH - 2*x - btnWidth*5)/4;
    __block float btnHeight = 55;
    __block float spaceY = 25;
    [self.dataAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (idx > 9) {
            return ;
        }
        NSDictionary *dict = obj;
        NSString *title = [dict objectForKey:@"PPMC00"];
        NSString *imageName = [dict objectForKey:@"LOGO00"];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame =  CGRectMake(x+idx%5*(btnWidth+spaceX), (idx/5)*(spaceY+btnHeight) + y, btnWidth, btnHeight);
        btn.tag = idx+BTNTAG;
        [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//        [btn setTitle:title forState:UIControlStateNormal];
////        [btn setImage:IMG(imageName) forState:UIControlStateNormal];
//        [btn sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, imageName)] forState:UIControlStateNormal];
//        [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
//        btn.titleLabel.font = FONT_SIZE(12);
        
//        UIImageView  *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btnWidth, btnHeight - 25)];
        UIImageView  *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((btnWidth-40)/2,0, 40,40)];
        [iconImageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, imageName)]];
        [btn addSubview:iconImageView];
        
        UILabel *titleLabel = [[UILabel alloc]  initWithFrame:CGRectMake(0, 45, btnWidth, 15)];
        titleLabel.font =  FONT_SIZE(12);
        titleLabel.textColor = Common_TextColor;
        titleLabel.textAlignment  = NSTextAlignmentCenter;
        titleLabel.text = title;
        [btn addSubview:titleLabel];
        
        
//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
//        //    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
//        [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height + 12 ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
//        [btn setImageEdgeInsets:UIEdgeInsetsMake( -btn.titleLabel.bounds.size.height, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
        
        [self.contentView addSubview:btn];
        
    }];
    
}

-(void)createConstraint
{
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.mas_equalTo(42);
    }];
    
}

- (CommonTitleView *)titleView
{
    if (!_titleView) {
        _titleView = [CommonTitleView new];

    }
    return _titleView;
    
}


- (NSArray *)dataAry
{
    if (!_dataAry) {
        _dataAry = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataAry;
}

@end
